{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d96ba7e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "username:kenny.l\n",
      "password:lzylove123.\n",
      "欢迎kenny.l登陆成功\n",
      "please input your money:1000000\n",
      "1 >>>> ('电脑1', 9500)\n",
      "2 >>>> ('电脑2', 800)\n",
      "3 >>>> ('法拉利', 8800000)\n",
      "4 >>>> ('python入门教程', 100)\n",
      "5 >>>> ('华为手机', 6000)\n",
      "选择购买商品编号[退出：t]：1\n",
      "('电脑1', 9500)\n",
      "1 >>>> ('电脑1', 9500)\n",
      "2 >>>> ('电脑2', 800)\n",
      "3 >>>> ('法拉利', 8800000)\n",
      "4 >>>> ('python入门教程', 100)\n",
      "5 >>>> ('华为手机', 6000)\n",
      "选择购买商品编号[退出：t]：t\n",
      "------------您已经购买如下商品----------------\n",
      "('电脑1', 9500)\n",
      "您还剩990500元\n"
     ]
    }
   ],
   "source": [
    "# 提示用户登录商店\n",
    "user1 = \"kenny.l\"\n",
    "password1 = \"lzylove123.\"\n",
    "\n",
    "for i in range(3,0,-1):   # 用户最多输入三次账户密码\n",
    "    username = input(\"username:\") \n",
    "    password = input(\"password:\")\n",
    "\n",
    "    #判断用户登录信息是否正确\n",
    "    if user1 == username and password1 == password:\n",
    "        print(\"欢迎\"+user1+\"登陆成功\")\n",
    "        break # 终止循环\n",
    "    \n",
    "    else:\n",
    "        if i == 1:\n",
    "            print(\"三次输入失败，请一小时后再来尝试\")\n",
    "        else:\n",
    "            print(\"账号不存在或密码不正确\")\n",
    "            print(\"您还有\"+str(i-1)+\"次2尝试的机会\") \n",
    "\n",
    "# 进入商店\n",
    "product_list=[\n",
    "    ('电脑1',9500),\n",
    "    ('电脑2',800),\n",
    "    ('法拉利',8800000),\n",
    "    ('python入门教程',100),\n",
    "    ('华为手机',6000),\n",
    " \n",
    "]\n",
    "saving=input('please input your money:')\n",
    "shopping_car=[]\n",
    "if saving.isdigit():\n",
    "    saving=int(saving)\n",
    "    while True:\n",
    "        #打印商品内容\n",
    "        for i,v in enumerate(product_list,1):\n",
    "            print(i,'>>>>',v)\n",
    " \n",
    "         #引导用户选择商品\n",
    "        choice=input('选择购买商品编号[退出：t]：')\n",
    " \n",
    "        #验证输入是否合法\n",
    "        if choice.isdigit():\n",
    "            choice=int(choice)\n",
    "            if choice>0 and choice<=len(product_list):\n",
    "                #将用户选择商品通过choice取出来\n",
    "                p_item=product_list[choice-1]\n",
    " \n",
    "                #如果钱够，用本金saving减去该商品价格，并将该商品加入购物车\n",
    "                if p_item[1]<saving:\n",
    "                    saving-=p_item[1]\n",
    " \n",
    "                    shopping_car.append(p_item)\n",
    " \n",
    "                else:\n",
    "                    print('余额不足，还剩%s'%saving)\n",
    "                print(p_item)\n",
    "            else:\n",
    "                print('编码不存在')\n",
    "        elif choice=='t':\n",
    "            print('------------您已经购买如下商品----------------')\n",
    "            #循环遍历购物车里的商品，购物车存放的是已买商品\n",
    "            for i in shopping_car:\n",
    "                print(i)\n",
    "            print('您还剩%s元'%saving)\n",
    "            break\n",
    "        else:\n",
    "            print('invalid input')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c857dc2c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3e6b8084",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
